package com.oncliedisk.administrator.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.util.Log;

import com.oncliedisk.administrator.model.BaseBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;


public class ReadDataFromContentProvider {
	
	public static void readImage(Context context) {
		ContentResolver resolver = context.getContentResolver() ;
		Uri imgUri = Images.Media.EXTERNAL_CONTENT_URI ;
		String[] columns = new String[]{
				Images.Media.TITLE , 
				Images.Media.DATA , 
				Images.Media.SIZE ,
				Images.Media.MIME_TYPE	// 类型
		};
		Cursor cursor = resolver.query(imgUri, columns, null, null, null) ;
		if(null != cursor && cursor.getCount() > 0) {
			while(cursor.moveToNext()) {
				Log.e("imgs" , cursor.getString(0) + "\t" + cursor.getString(3)) ;				
			}
		}
		if(null != cursor) {
			cursor.close() ;
		}
	}

	public static List<BaseBean> readAudio(Context context) {
		ContentResolver resolver = context.getContentResolver() ;
		Uri audioUri = Audio.Media.EXTERNAL_CONTENT_URI ;
		String[] columns = {
				Audio.Media.TITLE , 	// 歌名
				Audio.Media.DATA ,   	// 路径
				Audio.Media.ARTIST , 	// 演唱者
				Audio.Media.ALBUM	,	// 专辑名
				Audio.Media.DURATION , 	// 音频文件长度，毫秒
				Audio.Media.ALBUM_KEY  	// 用来读取专辑图片时使用
				
		};
		List<BaseBean> data = null ;
		Cursor cursor = resolver.query(
				audioUri, 
				columns, 
				//null, null,
				Audio.Media.DATA + " like ? or " + Audio.Media.DATA + " like ?" , new String[]{"%.mp3" , "%.wma"} ,
				null) ;
		if(null != cursor && cursor.getCount() > 0) {
			data = new ArrayList<BaseBean>() ;
			while(cursor.moveToNext()) {
				// 读到一个音频
				/////////////////// 读取该音频的专辑图片信息
				Bitmap bmp = null ;
				String albumKey = cursor.getString(5) ;
				Uri albumUri = Audio.Albums.EXTERNAL_CONTENT_URI ;
				Cursor albumCursor = resolver.query(albumUri, new String[]{Audio.Albums.ALBUM_ART}, Audio.Albums.ALBUM_KEY +"=?", new String[]{albumKey}, null) ;
				if(albumCursor != null && albumCursor.getCount() > 0) {
					albumCursor.moveToNext() ;
					String albumPath = albumCursor.getString(0);
					bmp = BitmapFactory.decodeFile(albumPath) ;
					albumCursor.close() ;
				}
				//////////////////
//				data.add(new MyAudio(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getLong(4), bmp)) ;
				String filepath = cursor.getString(1);
				BaseBean bean = new BaseBean();
				bean.setDisplayName(getFileName(filepath));
				bean.setPath(filepath);
//				bean.setMimeType(cursor.getString(cursor
//						.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
				data.add(bean);
			}
		}
		if(null != cursor) {
			cursor.close() ;
		}
		return data ;
	}

	public static List<BaseBean> getVideoList (Context context) {
		List<BaseBean> sysVideoList = new ArrayList<BaseBean>();
		// MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径
		String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
				MediaStore.Video.Thumbnails.VIDEO_ID };

		// MediaStore.Video.Media.DATA：视频文件路径；
		// MediaStore.Video.Media.DISPLAY_NAME : 视频文件名，如 testVideo.mp4
		// MediaStore.Video.Media.TITLE: 视频标题 : testVideo
		String[] mediaColumns = { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
				MediaStore.Video.Media.MIME_TYPE,
				MediaStore.Video.Media.DISPLAY_NAME };

		Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				mediaColumns, null, null, null);

		if(cursor==null){
			return null;
		}
		if (cursor.moveToFirst()) {
			do {
//				VideoInfo info = new VideoInfo();
				BaseBean info = new BaseBean();
				int id = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Video.Media._ID));
				Cursor thumbCursor = context.getContentResolver().query(
						MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
						thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
								+ "=" + id, null, null);
				if (thumbCursor.moveToFirst()) {
//					info.setThumbPath(thumbCursor.getString(thumbCursor
//							.getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
				}
				String path = cursor.getString(cursor
								.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
				File file = new File(path);
				if (file.exists()) {
					info.setPath(path);
//				info.setTitle(cursor.getString(cursor
//						.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)));

					info.setDisplayName(cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)));
//                LogUtil.log(TAG, "DisplayName:"+info.getDisplayName());
					info.setMimeType(cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
					sysVideoList.add(info);
				}



			}
			while (cursor.moveToNext());
		}
		return  sysVideoList;
	}


	public static List<BaseBean> getDocumentlist(Context context) {
    	Uri uri = MediaStore.Files.getContentUri("external");
    	
        String[] columns = new String[] {
                MediaStore.Files.FileColumns.TITLE,
				MediaStore.Files.FileColumns.DATA,
				MediaStore.Files.FileColumns.SIZE,
				MediaStore.Files.FileColumns.DATE_MODIFIED,
				MediaStore.Files.FileColumns.MIME_TYPE
        };// FileColumns._ID
        
        HashSet<String> sDocMimeTypesSet = new HashSet<String>() {
            {
                add("text/plain");
                add("text/plain");
                add("application/pdf");
                add("application/msword");
                add("application/vnd.ms-excel");
                add("application/vnd.ms-excel");
                add("application/vnd.ms-powerpoint");
            }
        };
        StringBuilder selectionBuilder = new StringBuilder();
        Iterator<String> iter = sDocMimeTypesSet.iterator();
        while(iter.hasNext()) {
        	selectionBuilder.append("(" + MediaStore.Files.FileColumns.MIME_TYPE + "=='" + iter.next() + "') OR ");
        }
        String selection = selectionBuilder.substring(0, selectionBuilder.lastIndexOf(")") + 1);
        
        
        Cursor cursor = context.getContentResolver().query(uri,columns, selection, null, null);
        List<BaseBean> list = new ArrayList<BaseBean>();
        if(cursor != null) {
        	while(cursor.moveToNext()) {
        		while(cursor.moveToNext()) {
        			//String str1 = cursor.getString(cursor.getColumnIndex(FileColumns._ID));
        			String str1 = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));//文件名
        			String str2 = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));//路径
        			String str3 = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE));//大小
        			String str4= cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATE_MODIFIED));
        			String str5= cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE));//格式
        			
        			BaseBean bean = new BaseBean();
        			bean.setDisplayName(getFileName(str2));
        			bean.setMimeType(str5);
        			bean.setPath(str2);
        			list.add(bean);
    			}
			}
        }
        return list;
    }

	public static String getFileName(String path) {
		File file = new File(path);

		return file.getName();
	}


}










